<template>
	<view class="personal-container">
		<image class="background-image" src="/static/image-my/Frame 9529.png" mode="scaleToFill"></image>

		<view class="user-card">
			<!-- 可点击修改的头像 -->
			<view class="avatar-wrapper" @click="changeAvatar">
				<image class="avatar" :src="userInfo.avatar" mode="aspectFill"></image>
				<view class="avatar-edit">
					<uni-icons type="camera" size="24" color="#fff"></uni-icons>
				</view>

			</view>
			<view class="detail-item">
				<text class="label">ID：</text>
				<text class="value">{{userInfo.id}}</text>
			</view>

			<view class="user-details">
				<view class="detail-item">
					<text class="label" style="margin-right: 50rpx;">昵称</text>
					<input class="values" v-model="editForm.nickname" placeholder="请输入昵称" />
				</view>
			</view>
		</view>

		<view class="action-buttons">
			<button class="save-btn" @click="saveProfile">保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					avatar: '/static/logo.png',
					nickname: '',
					id: ''
				},
				editForm: {
					nickname: '',
					avatar: ''
				}
			}
		},
		onLoad(options) {
			if (options.user) {
				const userData = JSON.parse(decodeURIComponent(options.user));
				this.userInfo = {
					...userData
				};
				this.editForm = {
					nickname: userData.nickname || '',
					avatar: userData.avatar || '/static/logo.png'
				};
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},

			// 修改头像
			changeAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.editForm.avatar = res.tempFilePaths[0];
						this.userInfo.avatar = res.tempFilePaths[0];
					}
				});
			},

			// 保存资料
			saveProfile() {
				if (!this.editForm.nickname.trim()) {
					uni.showToast({
						title: '请输入昵称',
						icon: 'none'
					});
					return;
				}

				// 更新用户信息
				const updatedUser = {
					...this.userInfo,
					nickname: this.editForm.nickname,
					avatar: this.editForm.avatar
				};

				// 模拟保存到服务器
				uni.showLoading({
					title: '保存中...'
				});

				setTimeout(() => {
					// 保存到本地存储
					uni.setStorageSync('userInfo', updatedUser);
					// 通知其他页面更新
					uni.$emit('updateUserInfo', updatedUser);

					uni.hideLoading();
					uni.showToast({
						title: '保存成功',
						icon: 'success'
					});

					// 返回上一页
					setTimeout(() => {
						uni.navigateBack();
					}, 1500);
				}, 1000);
			}
		}
	}
</script>

<style scoped>
	.background-image {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 400rpx;
		z-index: 0;
	}

	.personal-container {
		padding: 20rpx;
		background-color: rgb(247, 247, 247);
		min-height: 100vh;
	}

	.header {
		display: flex;
		align-items: center;
		padding: 20rpx;
		margin-bottom: 40rpx;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		margin-left: 20rpx;
	}

	.user-card {
		/* 	background-color: #fff; */
		/* border-radius: 20rpx; */
		padding: 40rpx;
		margin-bottom: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.avatar-wrapper {
		position: relative;
		margin-bottom: 40rpx;
	}

	.avatar {
		width: 150rpx;
		height: 150rpx;
		border-radius: 50%;
	}

	.avatar-edit {
		position: absolute;
		right: 0;
		bottom: 0;
		width: 50rpx;
		height: 50rpx;
		background-color: rgba(0, 0, 0, 0.5);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.user-details {
		width: 100%;
		background-color: #fff;
		border-radius: 22rpx;
		margin-top: 40rpx;
		padding: 15rpx;
	}

	.detail-item {
		display: flex;
		align-items: center;
	}

	.label {
		font-size: 30rpx;
		color: black;
		width: 60rpx;
		font-weight: 700;
	}

	.value {
		flex: 1;
		font-size: 30rpx;
		color: black;
	}

	.values {
		flex: 1;
		font-size: 30rpx;
		color: rgb(128, 135, 157);
	}

	input.value {
		border: none;
		outline: none;
		padding: 10rpx 0;
	}

	.action-buttons {
		display: flex;
		flex-direction: column;
		padding: 0 40rpx;
	}

	.save-btn {
		position: absolute;
		bottom: 100rpx;
		height: 80rpx;
		width: 85vw;
		line-height: 80rpx;
		font-size: 32rpx;
		border-radius: 40rpx;
		background-color: rgba(44, 122, 246);
		color: #fff;
	}
</style>